在製作攻擊或是放置物品等需要在特定情況下生成物件的功能時,就能用Instantiate來達成。
Instantiate(生成的物件,生成位置,物件方向);
待更
角色攻擊
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playcontrol : MonoBehaviour
{
public GameObject knife;//生成的物件
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
Instantiate(knife,this.transform.position,Quaternion.identity);//生成飛刀
}
}
}
飛刀射擊
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class knifecontrol : MonoBehaviour
{
public float timer=0;
void Start()
{
}
void Update()
{
this.gameObject.transform.position += new Vector3(Time.deltaTime * 60, 0, 0);//向右飛行
timer -= Time.deltaTime;
if (timer <= 0)
{
Destroy(this.gameObject);//刪除物件
}
}
}
待更
參考資料
https://www.youtube.com/watch?v=gm8uQ5WY-0E
https://docs.unity3d.com/cn/2018.4/ScriptReference/Object.Instantiate.html